//
//  WCMarketVC.m
//  CBT
//
//  Created by apple on 2019/7/8.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "WCMarketVC.h"
#import "FlashCell.h"
#import "WMHTMLViewController.h"
#import "WCDealOrderVC.h"
#import "WCOrderListModel.h"
#import "QuotesViewModel.h"

static NSString *identifier = @"cxCellID";
static CGFloat kMagin = 10.f;

@interface WCMarketVC ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) QuotesViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NoDataView *nodataView;
@property (nonatomic, assign) NSInteger page;

@end

@implementation WCMarketVC


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.title = kLocalizedString(@"商城", nil);
    
    [self.collectionView.mj_header beginRefreshing];
    
}

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    self.dataArray = [NSMutableArray array];

    [self outInput];
}

#pragma mark - OutInput
- (void)outInput
{
    @weakify(self);
    //行情列表请求成功
    [self.viewModel.quotesListSuccessObject subscribeNext:^(NSArray *listArray) {
        [self_weak_ stopLoading];
        if (self_weak_.page == 1) {
            [self_weak_.dataArray removeAllObjects];
        }
        [self_weak_.dataArray addObjectsFromArray:listArray];
        [self_weak_.nodataView removeFromSuperview];
        if ([NSArray isArrayEmpty:self_weak_.dataArray]) {
            self_weak_.nodataView.frame = CGRectMake(0, 0, ScreenNarrowSide, ScreenWideSide - self.tabBarController.tabBar.bounds.size.height);
            [self_weak_.collectionView addSubview:self_weak_.nodataView];
            self_weak_.nodataView.showType = ShowType_NoData;
        }else{
        }
        [self_weak_.collectionView.mj_header endRefreshing];
        [self_weak_.collectionView reloadData];
    }];
    
    //行情列表请求失败
    [self.viewModel.quotesListFailureObject subscribeNext:^(id x) {
        [self_weak_ stopLoading];
        [HFStatusView showFailureMessage:kLocalizedString(@"网络异常，请稍后重试", nil)];
        if ([NSArray isArrayEmpty:self_weak_.dataArray]) {
            self_weak_.nodataView.frame = CGRectMake(0, HFNavigationHeight, ScreenNarrowSide, ScreenWideSide - self.tabBarController.tabBar.bounds.size.height - HFNavigationHeight);
            [self_weak_.view addSubview:self_weak_.nodataView];
            self_weak_.nodataView.showType = ShowType_NoNetWork;
        }else{
            [self_weak_.nodataView removeFromSuperview];
        }
        [self_weak_.collectionView.mj_header endRefreshing];
        [self_weak_.collectionView reloadData];
    }];
}

#pragma mark - deleDate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FlashCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    WCOrderListModel *order = self.dataArray[indexPath.item];
    [cell.imgview lf_setImageURL:[NSString stringWithFormat:@"%@/%@",kBaseURL_PICSTATIC,order.image]];
    cell.titlelabel.text = [NSString stringWithFormat:@"%@",order.name];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    WCOrderListModel *order = self.dataArray[indexPath.item];

    FlashCell * cell = (FlashCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    WMHTMLViewController *aboutC = [[WMHTMLViewController alloc] init];
    aboutC.htmlUrl = [NSString stringWithFormat:@"%@?id=%@&lang=%@",order.address,order.orderId,[NetWork getAppLanguage]];
    CGFloat headH   = HFNavigationHeight;
    CGFloat statusH = HFTopStatusHeight;
    
    CGFloat bottomH = 0;
    if (statusH > 20) {
        bottomH     = 34;
    }
    
    aboutC.webViewRect  = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - bottomH - headH);
    aboutC.canScroll                = YES;
    aboutC.showProgressView         = YES;
    aboutC.hidesBottomBarWhenPushed = YES;
    
    aboutC.webViewTitle = cell.titlelabel.text;
    [self.navigationController pushViewController:aboutC animated:YES];
}

- (QuotesViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[QuotesViewModel alloc]init];
    }
    return _viewModel;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NoDataView *)nodataView
{
    if (!_nodataView) {
        @weak(self)
        _nodataView = [[NoDataView alloc]initWithFrame:CGRectMake(0, 0, ScreenNarrowSide, ScreenWideSide - self.tabBarController.tabBar.bounds.size.height) type:(ShowType_NoNetWork) clickBlock:^{
            [self_weak_ startClearBgColorLoading];
            self_weak_.page = 1;
            [self_weak_.viewModel exchangeDataWithPage:self_weak_.page];
        }];
    }
    return _nodataView;
}

#pragma mark - set_and_get
-(UICollectionView *)collectionView {
    if (!_collectionView) {
        //自动网格布局
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        CGFloat spaceWidth = (SCREEN_WIDTH - 300) / 3;
        //最小item间距（默认为10）
        flowLayout.minimumInteritemSpacing = spaceWidth;
        //设置单元格大小
        flowLayout.itemSize = CGSizeMake(150, 180);
        //最小行间距(默认为10)
        flowLayout.minimumLineSpacing = 40;
        
        //设置senction的内边距
        flowLayout.sectionInset = UIEdgeInsetsMake(kMagin, spaceWidth, spaceWidth, spaceWidth);
        //sectionHeader的大小,如果是竖向滚动，只需设置Y值。如果是横向，只需设置X值。
        //flowLayout.headerReferenceSize = CGSizeMake(100,0);
        //网格布局
        CGRect rect = self.view.bounds;
        CGFloat statusH = HFTopStatusHeight;
        if (statusH > 20) {
            rect.size.height -= 83+HFNavigationHeight;
        }
        else {
            rect.size.height -= 49+HFNavigationHeight;
        }
        _collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
        
        [_collectionView registerClass:[FlashCell class] forCellWithReuseIdentifier:identifier];
        //设置数据源代理
        _collectionView.dataSource = self;
        _collectionView.delegate   = self;
        
        __weak WCMarketVC *weakSelf = self;
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.page = 1;
            [weakSelf.viewModel exchangeDataWithPage:weakSelf.page];
            [weakSelf.collectionView.mj_header endRefreshing];
        }];
        
        [_collectionView.mj_header beginRefreshing];
        
        _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            weakSelf.page++;
            NSLog(@"%ld",(long)weakSelf.page);
            [weakSelf.viewModel exchangeDataWithPage:weakSelf.page];
            [weakSelf.collectionView.mj_footer endRefreshing];
        }];
        
        [_collectionView.mj_header beginRefreshing];
        
    }
    
    return _collectionView;
}


@end
